<template>
  <div class="role">
    <PageSearch
      :pageSearchConfig="pageSearchConfig"
      @reset="handleReset"
      @search="handleSearch"
    ></PageSearch>
    <PageContent
      class="pageContent"
      :pageContentConfig="refPageContentConfig"
      ref="pageContentRef"
      @createData="changeDialogVisible"
      @edit="handleEdit"
      @changePropListShow="changePropListShow"
    ></PageContent>
    <PageModal
      ref="pageModalRef"
      :pageModalConfig="pageModalConfig"
      :defaultInfo="defaultInfo"
      :other-info="otherInfo"
    >
      <template #uploadImg>
        <el-upload
          class="avatar-uploader"
          action="#"
          :show-file-list="false"
          :auto-upload="false"
          :on-change="change"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </template>
    </PageModal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import PageSearch from '@/components/page-search'
import PageContent from '@/components/page-content'
import PageModal from '@/components/page-modal'
import { pageSearchConfig } from './config/search'
import { pageContentConfig } from './config/content'
import { pageModalConfig } from './config/modal'
import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModal } from '@/hooks/usePageModal'
import { usePageContent } from '@/hooks/usePageContent'
const imageUrl = ref('')
const otherInfo = reactive({
  imgUrl: ''
})
//为什么放在这里来呢因为是搜索部分调用了content部分的方法
const [pageContentRef, handleReset, handleSearch] = usePageSearch()

// 响应式数据,方便子组件根据这个值的改变响应
const [refPageContentConfig, changePropListShow] =
  usePageContent(pageContentConfig)

// 新增数据的弹窗的显示,编辑数据的弹窗显示
const [pageModalRef, defaultInfo, changeDialogVisible, handleEdit] =
  usePageModal()

const change = (uploadFile: any) => {
  console.log(uploadFile)
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  console.log(imageUrl.value)
  otherInfo.imgUrl = uploadFile.raw
}
</script>

<style lang="less" scoped>
.role {
  .table {
    margin-top: 20px;
  }
  .pageContent {
    margin-top: 50px;
  }
}
</style>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
